<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="发货地" prop="startPoint">
        <el-select v-model="queryParams.startPoint" placeholder="请输入发货地" clearable>
          <el-option
            v-for="dict in dict.type.start_point"
            :key="dict.value"
            :label="dict.label"
            :value="Number(dict.value)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发货日期" prop="startDate">
        <el-date-picker clearable
                        v-model="queryParams.startDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择发货日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="船号" prop="shipNumber">
        <el-input
          v-model="queryParams.shipNumber"
          placeholder="请输入船号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收货单位" prop="consignee">
        <el-select v-model="queryParams.consignee" placeholder="请输入收货单位" clearable>
          <el-option
            v-for="dict in dict.type.consignee"
            :key="dict.value"
            :label="dict.label"
            :value="Number(dict.value)"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['app:traceStat:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['app:traceStat:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['app:traceStat:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['app:traceStat:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="traceStatList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="iid" />
      <el-table-column label="发货地" align="center" prop="startPoint">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.start_point" :value="scope.row.startPoint"/>
        </template>
      </el-table-column>
      <el-table-column label="发货日期" align="center" prop="startDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="船号" align="center" prop="shipNumber" />
      <el-table-column label="货物" align="center" prop="goods">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.goods" :value="scope.row.goods"/>
        </template>
      </el-table-column>
      <el-table-column label="收货单位" align="center" prop="consignee">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.consignee" :value="scope.row.consignee"/>
        </template>
      </el-table-column>
      <el-table-column label="收货日期" align="center" prop="receivingDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.receivingDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="吨位" align="center" prop="receivingTonnage" />
      <el-table-column label="单价" align="center" prop="price" />
      <el-table-column label="金额" align="center" prop="amout" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['app:traceStat:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['app:traceStat:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改运货统计对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="发货地" prop="startPoint">
          <el-select v-model="form.startPoint" placeholder="请输入发货地" clearable>
            <el-option
              v-for="dict in dict.type.start_point"
              :key="dict.value"
              :label="dict.label"
              :value="Number(dict.value)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="发货日期" prop="startDate">
          <el-date-picker clearable
                          v-model="form.startDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择发货日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="船号" prop="shipNumber">
          <el-input v-model="form.shipNumber" placeholder="请输入船号" />
        </el-form-item>
        <el-form-item label="货物" prop="goods">
          <el-select v-model="form.goods" placeholder="请输入发货地" clearable>
            <el-option
              v-for="dict in dict.type.goods"
              :key="dict.value"
              :label="dict.label"
              :value="Number(dict.value)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="收货单位" prop="consignee">
          <el-select v-model="form.consignee" placeholder="请输入收货单位" clearable>
            <el-option
              v-for="dict in dict.type.consignee"
              :key="dict.value"
              :label="dict.label"
              :value="Number(dict.value)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="收货日期" prop="receivingDate">
          <el-date-picker clearable
                          v-model="form.receivingDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择收货日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="吨位" prop="receivingTonnage">
          <el-input type="number" v-model="form.receivingTonnage" placeholder="请输入吨位" />
        </el-form-item>
        <el-form-item label="单价" prop="price">
          <el-input type="number" v-model="form.price" placeholder="请输入单价" />
        </el-form-item>
        <el-form-item label="金额" prop="amout">
          <el-input type="number" v-model="form.amout" placeholder="请输入金额" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTraceStat, getTraceStat, delTraceStat, addTraceStat, updateTraceStat } from "@/api/business/ship/traceStat";

export default {
  name: "TraceStat",
  dicts: ['goods', 'start_point', 'consignee'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 运货统计表格数据
      traceStatList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        startPoint: null,
        startDate: null,
        shipNumber: null,
        consignee: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        receivingDate: [
          { required: true, message: "收货日期不能为空", trigger: "blur" }
        ],
        receivingTonnage: [
          { required: true, message: "吨位不能为空", trigger: "blur" }
        ],
        price: [
          { required: true, message: "单价不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  watch: {
    'form.receivingTonnage'() {
      this.setAmout();
    },
    'form.price'() {
      this.setAmout();
    }
  },
  methods: {
    setAmout() {
      if (this.form.receivingTonnage !== null && this.form.price !== null) {
        let data = String(this.form.receivingTonnage * this.form.price); // 将结果转换为字符串类型
        this.$nextTick(() => {
          this.form.amout = data; // 手动更新 form.addAllTime 的值
        });
      }
    },
    /** 查询运货统计列表 */
    getList() {
      this.loading = true;
      listTraceStat(this.queryParams).then(response => {
        this.traceStatList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        iid: null,
        startPoint: null,
        startDate: null,
        shipNumber: null,
        goods: null,
        consignee: null,
        receivingDate: null,
        receivingTonnage: null,
        price: null,
        amout: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.iid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加运货统计";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const iid = row.iid || this.ids
      getTraceStat(iid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改运货统计";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.iid != null) {
            updateTraceStat(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTraceStat(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const iids = row.iid || this.ids;
      this.$modal.confirm('是否确认删除运货统计编号为"' + iids + '"的数据项？').then(function() {
        return delTraceStat(iids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('app/traceStat/export', {
        ...this.queryParams
      }, `traceStat_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
